# -*- coding: utf-8 -*- 
"""
@Author : Chan ZiWen
@Date : 2022/6/17 09:33
File Description:
Columns: [
    gdl_zdfw_sc_bluetooth_device.log_id,
    gdl_zdfw_sc_bluetooth_device.tv_mac,
    gdl_zdfw_sc_bluetooth_device.active_id,
    gdl_zdfw_sc_bluetooth_device.bluetooth_name,
    gdl_zdfw_sc_bluetooth_device.mac,
    gdl_zdfw_sc_bluetooth_device.trade_name,
    gdl_zdfw_sc_bluetooth_device.device_name,
    gdl_zdfw_sc_bluetooth_device.create_time,
    gdl_zdfw_sc_bluetooth_device.rssi,
    gdl_zdfw_sc_bluetooth_device.distance,
    gdl_zdfw_sc_bluetooth_device.log_time,
    gdl_zdfw_sc_bluetooth_device.day
    ]
log_id,tv_mac,active_id,mac,rssi,distance,log_time,day,sum(mac)
存入流程：

        1， 获取到每天计算好的结果，
        2， 从hive结果表中读取每个15天的分析结果
        3，
mode tv_mac active_id bluetooth_name mac current_time datetime final_time log_time

"""
import datetime
import os, sys
import time
import yaml
import json
import requests
import numpy as np
import vthread
import pandas as pd
import pyspark
from pyhive import hive

# work_dir = os.path.dirname(__file__)
# os.chdir(work_dir)


def Connect(configs):
    con = hive.Connection(
                host=configs['host'],
                port=configs['port'],
                auth='LDAP',
                username=configs['user'],
                password=configs['password'],
                database=configs['database'])
    return con


def num_out(data):
    data = str(data)+'\t'
    return data


def post(date, datas, url, headers):
    datas = json.dumps(datas)
    response = json.loads(requests.post(url, data=datas, headers=headers).text)
    if response['code'] != 1000:
        raise RuntimeError(f" {date}  ({response})")


if __name__ == '__main__':
    data = [{'activeId': '96586757', 'analysisDatetime': '2022-07-04', 'mobileMac': '64C582BEDB79'},
            {'activeId': '96592742', 'analysisDatetime': '2022-07-04', 'mobileMac': '782E56A1A7D1'},
            {'activeId': '96596793', 'analysisDatetime': '2022-07-04', 'mobileMac': '8CA6DF231D83'},
            {'activeId': '96598120', 'analysisDatetime': '2022-07-04', 'mobileMac': 'C43960050D89'},
            {'activeId': '96602441', 'analysisDatetime': '2022-07-04', 'mobileMac': 'F46D2F3A279E'},
            {'activeId': '96607475', 'analysisDatetime': '2022-07-04', 'mobileMac': '4C8120420F3E'},
            {'activeId': '96633046', 'analysisDatetime': '2022-07-04', 'mobileMac': '9CB2E81DF197'},
            {'activeId': '96634409', 'analysisDatetime': '2022-07-04', 'mobileMac': '8CDC02EAF3AE'},
            {'activeId': '96642585', 'analysisDatetime': '2022-07-04', 'mobileMac': 'C83A353E4A50'},
            {'activeId': '96682799', 'analysisDatetime': '2022-07-04', 'mobileMac': 'BC5FF62E76AE'},
            {'activeId': '96720000', 'analysisDatetime': '2022-07-04', 'mobileMac': '7CB59B2DCF73'},
            {'activeId': '96735181', 'analysisDatetime': '2022-07-04', 'mobileMac': '1010814886E0'},
            {'activeId': '96745280', 'analysisDatetime': '2022-07-04', 'mobileMac': '047E23106C70'},
            {'activeId': '96748792', 'analysisDatetime': '2022-07-04', 'mobileMac': '408C1F05A077'},
            {'activeId': '96760142', 'analysisDatetime': '2022-07-04', 'mobileMac': '646E9768FFB3'},
            {'activeId': '96770637', 'analysisDatetime': '2022-07-04', 'mobileMac': 'CC2D21AC3D38'},
            {'activeId': '96779056', 'analysisDatetime': '2022-07-04', 'mobileMac': '5013955DF231'}]
    # url = "http://172.20.151.90:8093/conf/mobile/mac/saveBatch"
    url = "http://beta-life-assistant.skysrt.com/conf/mobile/mac/saveBatch"
    headers = {"Content-Type": "application/json"}
    date = '2022-07-04'
    post(date, data, url, headers)
